<template>
  <div class="order-confirm">
    <div v-if="loading" class="loading">
      加载中...
    </div>
    <template v-else>
      <div class="header">
        <div class="back" @click="$router.back()">
          <i class="iconfont">&#xe697;</i> 返回
        </div>
        <h2>订单确认</h2>
      </div>

      <div class="content">
        <div class="movie-info">
          <img :src="movie.poster" :alt="movie.name">
          <div class="details">
            <h3>{{ movie.name }}</h3>
            <p>{{ showtime.startTime }}</p>
            <p>{{ showtime.language }} {{ showtime.dimension }}</p>
            <p>{{ showtime.hallName }}</p>
          </div>
        </div>

        <div class="seats-info">
          <h4>已选座位</h4>
          <div class="seats">
            <span v-for="seat in formattedSeats" :key="seat" class="seat-tag">
              {{ seat }}
            </span>
          </div>
        </div>

        <div class="price-info">
          <div class="price-item">
            <span>票价</span>
            <span>￥{{ showtime.price }} × {{ selectedSeats.length }}</span>
          </div>
          <div class="total">
            <span>总计</span>
            <span class="amount">￥{{ totalPrice }}</span>
          </div>
        </div>

        <div class="notice">
          <h4>观影须知</h4>
          <p>1. 请提前15分钟到达影院取票</p>
          <p>2. 开场前30分钟停止售票</p>
          <p>3. 影院内禁止携带外部食品</p>
        </div>
      </div>

      <div class="bottom-bar">
        <div class="price">
          <span class="label">实付金额：</span>
          <span class="amount">￥{{ totalPrice }}</span>
        </div>
        <button 
          class="submit-btn" 
          :disabled="submitting"
          @click="submitOrder"
        >
          {{ submitting ? '提交中...' : '确认支付' }}
        </button>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { movieApi } from '@/api/movie'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()

const loading = ref(true)
const submitting = ref(false)
const movie = ref({})
const showtime = ref({})
const selectedSeats = ref([])

// 格式化座位号
const formattedSeats = computed(() => {
  return selectedSeats.value.map(seat => 
    `${String.fromCharCode(65 + seat.row)}${seat.col + 1}`
  )
})

// 计算总价
const totalPrice = computed(() => {
  return (showtime.value.price * selectedSeats.value.length).toFixed(2)
})

onMounted(async () => {
  try {
    // 从路由参数中获取数据
    const { showtimeId, seats } = route.query
    selectedSeats.value = JSON.parse(seats)

    // 获取电影和场次信息
    const [movieRes, showtimeRes] = await Promise.all([
      movieApi.getMovieDetail(route.params.movieId),
      movieApi.getShowtimeDetail(showtimeId)
    ])

    movie.value = movieRes.data
    showtime.value = showtimeRes.data
  } catch (error) {
    console.error('获取订单数据失败:', error)
    ElMessage.error('获取订单数据失败，请重试')
  } finally {
    loading.value = false
  }
})

const submitOrder = async () => {
  if (submitting.value) return
  
  submitting.value = true
  try {
    const orderRes = await movieApi.createOrder(
      route.query.showtimeId,
      selectedSeats.value
    )
    
    // 跳转到支付页面
    router.push({
      name: 'payment',
      params: { orderId: orderRes.data.orderId }
    })
  } catch (error) {
    console.error('创建订单失败:', error)
    ElMessage.error('创建订单失败，请重试')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.order-confirm {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 60px;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #999;
}

.header {
  background: white;
  padding: 15px;
  margin-bottom: 10px;
}

.back {
  color: #666;
  cursor: pointer;
  margin-bottom: 10px;
}

.header h2 {
  margin: 0;
}

.content {
  background: white;
  padding: 20px;
}

.movie-info {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.movie-info img {
  width: 100px;
  height: 140px;
  object-fit: cover;
  border-radius: 4px;
}

.details h3 {
  margin: 0 0 10px 0;
}

.details p {
  margin: 5px 0;
  color: #666;
}

.seats-info {
  margin-bottom: 20px;
}

.seats-info h4 {
  margin: 0 0 10px 0;
}

.seats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.seat-tag {
  padding: 5px 10px;
  background: #f0f0f0;
  border-radius: 4px;
}

.price-info {
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  padding: 15px 0;
  margin-bottom: 20px;
}

.price-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.total {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
}

.amount {
  color: #ff5f16;
}

.notice {
  color: #666;
}

.notice h4 {
  margin: 0 0 10px 0;
}

.notice p {
  margin: 5px 0;
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
}

.submit-btn {
  padding: 10px 30px;
  background: #ff5f16;
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 16px;
  cursor: pointer;
}

.submit-btn:disabled {
  background: #ffaa83;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .content {
    padding: 15px;
  }
  
  .movie-info img {
    width: 80px;
    height: 112px;
  }
  
  .details h3 {
    font-size: 16px;
  }
  
  .details p {
    font-size: 14px;
  }
}
</style> 